#ifndef GLOBAL_H
#define GLOBAL_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include <alsa/asoundlib.h>
#include <errno.h>
#include "memory.h"

extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avio.h"
#include "libavutil/avutil.h"
//#include "libavutil/log.h"
//#include "libavformat/avformat.h"
//#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
//#include "libavfilter/avfilter.h"
//#include "libavutil/avstring.h"
//#include "libavutil/pixdesc.h"
//#include "libavutil/imgutils.h"
//#include "libavutil/dict.h"
//#include "libavutil/parseutils.h"
//#include "libavutil/samplefmt.h"
//#include "libavutil/opt.h"
//#include "libavcodec/avfft.h"
//#include "libavfilter/avfilter.h"
//#include "libavfilter/avfiltergraph.h"

//#include "libavutil/mathematics.h"
}

#include "SDL/SDL.h"
#include "SDL/SDL_thread.h"

#define SDL_AUDIO_U8    0x0008
#define SDl_AUDIO_S8    0x8008
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define SDL_AUDIO_S16 0x8010
#else
#define SDL_AUDIO_S16 0x9010
#endif

#define SDL_MAX_BUFFER_OF_SOUNDS (8 * 1024 * 1024)  //音频
#define UDP_MAX_DATA_BUFFER (8 * 1024 * 1024)   //数据缓冲区大小

#define _DEVICE_NAME "/dev/video0"
#define CLEAR(x) memset(&(x), 0, sizeof(x))
#define _CAPTURE_WIDTH 720
#define _CAPTURE_HEIGHT 576
#define _DISPLAY_WIDTH 1024
#define _DISPLAY_HEIGHT 768

typedef struct _SDLMODULE
{
    int s32WindowWidth, s32WindowHeight;
    SDL_Surface *ptSdlWindow;

    int s32YuvWidth;
    int s32YuvHeight;
    SDL_Overlay *ptYuvLayer;

    struct SwsContext *ptSwsContextV;
    AVFrame *pFrame;
    uint8_t *pu8BufferDecodeV;

    SDL_AudioSpec tSdlAudioSpec;
    int s32Volume, cap_flag;
    SEMAPHOREHANDLE hAudioSemHandle;
    uint8_t *pu8AudioFifo, *pu8BufferAudio;
    void *pvCallBackAudio;
    void *pvUserDataAudio;
}SDLMODULE, *PSDLMODULE;

typedef struct _TAVSDLYUVSURFACE
{
    int s32DisplayX;
    int s32DisplayY;
    int s32DisplayWidth;
    int s32DisplayHeight;

    PSDLMODULE ptSdlModule;
}TAVSDLYUVSURFACE, *PTAVSDLYUVSURFACE;


void SdlAudioMixer(void *pvUserData, Uint8 *pu8Buffer, int s32Length);
PSDLMODULE SdlModuleInit(uint32_t u32WindowId, int s32WindowWidth, int s32WindowHeight, int s32YuvWidth, int s32YuvHeight,
                          int s32Frequency, int s32Format, int s32Channel, int volume);
void SdlModuleUnInit(PSDLMODULE ptSdlModule);
PTAVSDLYUVSURFACE AvSDLYUVCreate(PSDLMODULE ptSdlModule,
                              int s32DisplayX, int s32DisplayY, int s32DisplayWidth, int s32DisplayHeight);
void AvSDLYUVDestroy(void *pvHandle);
int AvSDLYUVDisplay(PTAVSDLYUVSURFACE pvHandle, uint8_t *pu8Buffer, int s32Length);
void * AvSdlAudioCreate(PSDLMODULE ptSdlModule, int s32Volume, void *pvCallBack, void *pvUserData);
void AvSdlAudioStart(void * pvHandle);
void AvSdlAudioStop(void * pvHandle);
void AvSdlAudioDestroy(void * pvHandle);

void *ThreadReadAndDispaly(void *pVoid);
void *ThreadRecordPcm( void *pVoid);

#endif // GLOBAL_H
